package com.bhkj.pdjhforotherapp.core.tip;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bhkj.pdjhforotherapp.R;
import com.bhkj.pdjhforotherapp.common.Contact;
import com.bhkj.pdjhforotherapp.common.bean.RePrintBean;
import com.bhkj.pdjhforotherapp.common.callback.CallbackManager;
import com.bhkj.pdjhforotherapp.common.callback.IGlabolCallback;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;

import java.util.Timer;
import java.util.TimerTask;

@SuppressWarnings("rawtypes")
public class TipHandler implements View.OnClickListener {
    ITipView iTipView;
    private Timer timer;
    private TimerTask timerTask;
    private int count = 10;
    private RePrintBean rePrintBean;

    public TipHandler(ITipView iTipView) {
        this.iTipView = iTipView;
        init();
    }


    /**
     * 初始化
     */
    private void init() {
        TextView tipTitleTv = iTipView.findViewById(R.id.tv_activity_tip_title);
        TextView tipContentTv = iTipView.findViewById(R.id.tv_activity_tip_content);
        TextView tipCountDownTv = iTipView.findViewById(R.id.tv_activity_tip_countdown);
        tipCountDownTv.setText(count + "秒");
        Button tipReprintBt = iTipView.findViewById(R.id.bt_activity_reprint);
        tipReprintBt.setOnClickListener(this);
        Button tipCloseprintBt = iTipView.findViewById(R.id.bt_activity_closeprint);
        tipCloseprintBt.setOnClickListener(this);

        Intent intent = iTipView.getIntent();
        if (intent != null) {
            String tipTitle = intent.getStringExtra(Contact.TIP_TITLE);
            String tipContent = intent.getStringExtra(Contact.TIP_CONTENT);
            String printStr = intent.getStringExtra(Contact.ID_NAME);
            if (ObjectUtils.isNotEmpty(printStr)) {
                String[] printStrs = printStr.split("-");
                rePrintBean = new RePrintBean(printStrs[0], printStrs[1], printStrs[2], printStrs[3], printStrs[4]);
            }

            boolean isRePrint = intent.getBooleanExtra(Contact.TIP_REPRINT, false);
            tipTitleTv.setText(tipTitle);
            tipContentTv.setText(tipContent);
            int rePrintTip;
            if (isRePrint) {
                rePrintTip = R.string.reprint;
                tipCloseprintBt.setVisibility(View.VISIBLE);
            } else {
                rePrintTip = R.string.sure;
            }
            tipReprintBt.setText(rePrintTip);

            initCountDown(tipCountDownTv);
        }
    }

    private void initCountDown(TextView tipCountDownTv) {
        if (timer == null) {
            timer = new Timer();
        }
        if (timerTask == null) {

            timerTask = new TimerTask() {
                @Override
                public void run() {
                    Activity topActivity = ActivityUtils.getTopActivity();
                    if (topActivity != null) {
                        topActivity.runOnUiThread(() -> tipCountDownTv.setText(count + "秒"));
                    }
                    if (count == 1) {
                        cancelTask();
                        iTipView.finish();
                    }
                    count--;
                }
            };
        }
        timer.schedule(timerTask, 0, 1000);
    }

    private void cancelTask() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }

        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }

        CallbackManager instance = CallbackManager.getInstance();
        IGlabolCallback callback = instance.getCallback(Contact.CLOSE_READ_ID);
        if (callback != null) {
            callback.executeCallback("");
        }
    }

    @Override
    public void onClick(View v) {
        Button bt = (Button) v;
        if (bt.getText().equals(StringUtils.getString(R.string.sure))) {
            cancelTask();
            iTipView.finish();
        } else if (bt.getText().equals(StringUtils.getString(R.string.reprint))) {
            IGlabolCallback callback = CallbackManager.getInstance().getCallback(Contact.TIP_REPRINT);
            if (callback != null) {
                callback.executeCallback(rePrintBean);
            }
            iTipView.finish();
        } else {
            IGlabolCallback callback = CallbackManager.getInstance().getCallback(Contact.CLOSE_READ_ID);
            if (callback != null) {
                callback.executeCallback("");
            }
            iTipView.finish();
        }
    }


}
